const {
	resolve
} = require("path")




/*
 * @Author: zcpark
 * @Date: 2023-09-17 00:41:45
 * @LastEditors: zcpark
 * @LastEditTime: 2023-09-19 19:49:20
 * @FilePath: /juyida-uniapp/common/util/uniGet.js
 * @Description: 
 */
let gdKey = 'ad684a15ebbb6f5d12c407a3d593c949' 

function model(){
	uni.getSystemInfo({
		success: function (res) {
			if(res.platform == "ios"){
				gdKey = 'd8b84cf540bcca9928db69b0119fc152'
			}else if(res.platform == "android"){
				gdKey = 'fd84f5604abaf0ba422155ac51adaf00'
			}
		 
		}
	});
}
function autonavi(data, callback) {
	//逆编码获取省份
	if (!data) {
		uni.$u.toast("请选择位置信息");
		return;
	}
	uni.request({
		url: 'https://restapi.amap.com/v3/geocode/regeo?',
		method: 'get',
		data: { //参数
			location:data.longitude+',' + data.latitude,
			
			
			key: gdKey,
		},
		success: (res) => { 
			let dress = res.data.regeocode.addressComponent
			let detailDress = dress.district  
			
			callback(detailDress, dress)
		}
	})
}

function POI(province, citys, strCity, callback) { // 经纬度详情获取

	uni.request({
		url: 'https://restapi.amap.com/v3/place/text?',
		method: 'get',
		data: { //参数
			keywords: citys,
			city: province,
			key:gdKey,
		},
		success: (res) => {
			if (res.data.status == '0') { //如果限额将返回本地当前位置信息
				this.getLocation((resl) => {
					callback(resl, '----》')
				})
				return
			}
			console.log(res, '----')
			let Numstr = res.data.pois[0].location.split(',')
			callback(Numstr, province + strCity)
		}
	})
}


//导航 路径规划
function planning(data, callback) {
	let url =
		`uri.amap.com/navigation?from=${data.startpoint.longitude},${data.startpoint.latitude},我的位置&to=${data.endpoint.longitude},${data.endpoint.latitude},${data.endpoint.startPointDetail}&via=&mode=car&policy=1&src=mypage&coordinate=gaode&callnative=0`;
	// #ifdef APP-PLUS
	plus.runtime.openURL('https://' + url);
	// #endif

	// #ifndef APP-PLUS
	window.open(url);
	// #endif
}

function getLocation(callback) { //获取我的当前位置信息
	uni.getLocation({
		type: 'gcj02',
		isHighAccuracy: true,
		success: function(res) {
			let getLocations = {
				longitude: res.longitude,
				latitude: res.latitude
			}
			console.log(res)
			callback(getLocations)
		},
		fail: function(e) {
			 console.log(e)
			alert(e)
		}
	});
}

function getTime() { // 获取当前时间啊
	var date = new Date();
	var sign2 = ":";
	var year = date.getFullYear() // 年
	var month = date.getMonth() + 1; // 月
	var day = date.getDate(); // 日
	var hour = date.getHours(); // 时
	var minutes = date.getMinutes(); // 分
	var seconds = date.getSeconds() //秒
	var weekArr = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期天'];
	var week = weekArr[date.getDay()];
	// 给一位数的数据前面加 “0”
	if (month >= 1 && month <= 9) {
		month = "0" + month;
	}
	if (day >= 0 && day <= 9) {
		day = "0" + day;
	}
	if (hour >= 0 && hour <= 9) {
		hour = "0" + hour;
	}
	if (minutes >= 0 && minutes <= 9) {
		minutes = "0" + minutes;
	}
	if (seconds >= 0 && seconds <= 9) {
		seconds = "0" + seconds;
	}
	let dateTimer = year + "-" + month + "-" + day + " " + hour + sign2 + minutes + sign2 + seconds;
	return dateTimer
}
//日期字符串转成时间戳
//例如var date = '2015-03-05 17:59:00.0';
function dateStrChangeTimeTamp(dateStr) {
	const timestamp = new Date(dateStr).getTime();
	return timestamp
}

// model()
module.exports = {
	autonavi: autonavi,
	planning: planning,
	getLocation: getLocation,
	POI: POI,
	dateStr: dateStrChangeTimeTamp,
	getTime:getTime
}